//
//  TeamMemoryViewController.m
//  UEAssistant
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "ShareMemoryViewController.h"

@interface ShareMemoryViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSMutableArray                  *_dataSource;
    NSMutableArray                  *_userIdArr;
}

@property (nonatomic, strong) UILabel               *NaviTitle;
@property (nonatomic, strong) UITableView           *tableView;
@property (nonatomic, strong) UILabel               *setTitle;
@property (nonatomic, strong) UILabel               *subTitle;
@property (nonatomic, strong) UISwitch              *swichV;
@property (nonatomic, strong) UILabel               *useLabel;
@property (nonatomic, strong) UIButton              *selectBt;
@property (nonatomic, assign) NSInteger             count;

@end

@implementation ShareMemoryViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self.NaviTitle setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [self.NaviTitle setHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self LoadData];
    [self layoutUI];
}

- (void)layoutUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:self.NaviTitle];
    [self.view addSubview:self.tableView];
}

- (void)LoadData{
    
    _userIdArr = [NSMutableArray array];
    
    [LSProgressHUD showOnWindow];
    NSDictionary *params = @{@"projectId":shareData.product_Id,
                             @"corpusType":@"share",@"page":@"1"};
    [AvailableNetWorking postWithUrl:checkCorpus_connector params:params successHandler:^(id responseObject) {
        
        NSLog(@"data == %@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            _dataSource = [NSMutableArray arrayWithArray:responseObject[@"result"]];
            [self.tableView reloadData];
            [LSProgressHUD hideForWindow];
        }
    } falureHandler:^(NSError *error) {
        
    }];

}

- (void)refreshShareListdata{

    NSDictionary *params = @{@"projectId":shareData.product_Id,
                             @"corpusType":@"share",@"page":@"1"};
    [AvailableNetWorking postWithUrl:checkCorpus_connector params:params successHandler:^(id responseObject) {
        
        NSLog(@"data == %@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            _dataSource = [NSMutableArray arrayWithArray:responseObject[@"result"]];
            [self.tableView reloadData];
            self.view.userInteractionEnabled = YES;
            [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"操作成功" andTitleColor:[UIColor whiteColor] andCancel:NO];
        }
    } falureHandler:^(NSError *error) {
        
    }];

}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    else{
    
        if ([detailsModel.shareIsAll integerValue] == 0) {
            return _dataSource.count;

        }
        else{
        
            return 0;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 30*Radio;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        return 40*Radio;
    }
    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 30*Radio)];
        backView.backgroundColor = RGB(247, 247, 247, 1);
        return backView;
    }
    UIView *view = nil;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40*Radio)];
        backView.backgroundColor = RGB(247, 247, 247, 1);
        return backView;
    }
    UIView *view = nil;
    return view;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        [cell addSubview:self.setTitle];
        [cell addSubview:self.subTitle];
        [cell addSubview:self.swichV];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.textLabel.text = _dataSource[indexPath.row][@"tmx_dbname"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ -> %@",_dataSource[indexPath.row][@"sourcelangname"],_dataSource[indexPath.row][@"targetlangname"]];
    cell.textLabel.textColor = RGB(51, 51, 51, 1);
    cell.detailTextLabel.textColor = RGB(153, 153, 153, 1);
    cell.textLabel.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:FLEXIBLE(12)];
    
    _useLabel = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width-80*Radio, 20*Radio, 30*Radio, 20*Radio)];
    _useLabel.backgroundColor = [UIColor clearColor];
    _useLabel.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _useLabel.textColor = RGB(143, 143, 143, 1);
    _useLabel.text = @"使用";
    [cell addSubview:_useLabel];
    
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.frame = CGRectMake(Screen_Width-50*Radio, 20*Radio, 20, 20);
    if ([[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUse"]] isEqualToString:@"1"]) {
        [_selectBt setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    }else{
        [_selectBt setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
    }
    [_selectBt addTarget:self action:@selector(selectToolsClied_use:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:_selectBt];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


#pragma mark -- Event

-(void)switchAction:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    
    [_userIdArr removeAllObjects];
    [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
            [_userIdArr addObject:obj[@"tmx_dbid"]];
        }
    }];
    NSString *IdString = [_userIdArr componentsJoinedByString:@","];

    if (isButtonOn) {
   
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":@"",
                                 @"corpusType":@"share",
                                 @"ifallshare":@"1"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            detailsModel.shareIsAll = @"1";
            [self refreshShareListdata];
            
        } falureHandler:^(NSError *error) {
            
        }];
        
    }else {
        
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":@"",
                                 @"corpusType":@"share",
                                 @"ifallshare":@"0"};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            detailsModel.shareIsAll = @"0";
            [self refreshShareListdata];
            
        } falureHandler:^(NSError *error) {
            
        }];
    }
}

- (void)selectToolsClied_use:(UIButton *)sender{
    
    self.view.userInteractionEnabled = NO;
    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [_userIdArr removeAllObjects];
    
    if ([[NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"isUse"]] isEqualToString:@"1"]) {
        
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tmx_dbid"]];
            }
        }];
        [_userIdArr removeObject:_dataSource[indexPath.row][@"tmx_dbid"]];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":@"",
                                 @"corpusType":@"share",
                                 @"ifallshare":detailsModel.shareIsAll};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            [self refreshShareListdata];
            
        } falureHandler:^(NSError *error) {
            
        }];

    }
    else{
        [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([[NSString stringWithFormat:@"%@",obj[@"isUse"]] isEqualToString:@"1"]) {
                [_userIdArr addObject:obj[@"tmx_dbid"]];
            }
        }];
        [_userIdArr addObject:_dataSource[indexPath.row][@"tmx_dbid"]];
        NSString *IdString = [_userIdArr componentsJoinedByString:@","];
        NSDictionary *params = @{@"projectId":shareData.product_Id,
                                 @"idList":IdString,
                                 @"updatecorpusId":@"",
                                 @"corpusType":@"share",
                                 @"ifallshare":detailsModel.shareIsAll};
        
        [AvailableNetWorking postWithUrl:savelangKitInfo_connector params:params successHandler:^(id responseObject) {
            
            NSLog(@"rrsssss == %@",responseObject);
            [self refreshShareListdata];
            
        } falureHandler:^(NSError *error) {
            
        }];

    }
}

#pragma mark -- getter
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = RGB(247, 247, 247, 1);
        _tableView.rowHeight = 60*Radio;
    }
    return _tableView;
}

- (UILabel *)NaviTitle{
    
    if (!_NaviTitle) {
        _NaviTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
        _NaviTitle.center = CGPointMake(self.view.center.x, 20);
        _NaviTitle.backgroundColor = [UIColor clearColor];
        _NaviTitle.text = @"共享记忆库";
        _NaviTitle.textAlignment = NSTextAlignmentCenter;
        _NaviTitle.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _NaviTitle;
}

- (UILabel *)setTitle{
    
    if (!_setTitle) {
        _setTitle = [[UILabel alloc]initWithFrame:CGRectMake(15*Radio, 15*Radio, 150*Radio, 20*Radio)];
        _setTitle.backgroundColor = [UIColor clearColor];
        _setTitle.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
        _setTitle.textColor = RGB(51, 51, 51, 1);
        _setTitle.text = @"使用所有共享记忆库";
    }
    return _setTitle;
}

- (UILabel *)subTitle{
    
    if (!_subTitle) {
        _subTitle = [[UILabel alloc]initWithFrame:CGRectMake(15*Radio, 35*Radio, 300*Radio, 15*Radio)];
        _subTitle.backgroundColor = [UIColor clearColor];
        _subTitle.font = [UIFont systemFontOfSize:FLEXIBLE(12)];
        _subTitle.textColor = RGB(153, 153, 153, 1);
        _subTitle.text = @"如果不知道如何选择，请使用默认设置即可";
    }
    return _subTitle;
}

- (UISwitch *)swichV{
    
    if (!_swichV) {
        _swichV = [[UISwitch alloc]initWithFrame:CGRectMake(Screen_Width-70*Radio, 15.0f, 100.0f, 30.0f)];
        if ([detailsModel.shareIsAll integerValue] == 0) {
            _swichV.on = NO;
        }else{
            _swichV.on = YES;
        }
        [_swichV addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        _swichV.onTintColor = mainColor;
    }
    return _swichV;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
